package com.itheima.mall.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.mall.client.PmsProductClient;
import com.itheima.mall.mapper.FlashPromotionProductRelationMapper;
import com.itheima.mall.pojo.FlashPromotionProductRelation;
import com.itheima.mall.pojo.Product;
import com.itheima.mall.service.SmsFlashPromotionProductRelationService;
import com.itheima.mall.vo.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class SmsFlashPromotionProductRelationServiceImpl extends ServiceImpl<FlashPromotionProductRelationMapper,FlashPromotionProductRelation> implements SmsFlashPromotionProductRelationService {

    @Autowired
    private FlashPromotionProductRelationMapper flashPromotionProductRelationMapper;

    @Autowired
    private PmsProductClient pmsProductClient;

    /**
     * 批量选择商品添加关联
     */
    @Override
    public void create(List<FlashPromotionProductRelation> relationList) {
        //直接添加fppr表
        this.saveBatch(relationList);
    }

    /**
     * 删除关联
     */
    @Override
    public void deleteRelation(Long id) {
        //直接删
        this.removeById(id);
    }

    /**
     * 分页查询不同场次关联及商品信息
     */
    @Override
    public PageResult list(Integer flashPromotionId, Integer flashPromotionSessionId, Integer pageNum, Integer pageSize) {
        Page<FlashPromotionProductRelation> page = new Page<>(pageNum,pageSize);
        //根据主键查fppr表
        LambdaQueryWrapper<FlashPromotionProductRelation> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(flashPromotionId != null,FlashPromotionProductRelation::getFlashPromotionId,flashPromotionId)
                .eq(flashPromotionSessionId != null,FlashPromotionProductRelation::getFlashPromotionSessionId,flashPromotionSessionId);
        Page<FlashPromotionProductRelation> pageInfo = flashPromotionProductRelationMapper.selectPage(page, queryWrapper);
        List<FlashPromotionProductRelation> records = pageInfo.getRecords();
        if (CollUtil.isEmpty(records)){
            return new PageResult();
        }
        //提取pId
        List<Long> pIds = records.stream().map(FlashPromotionProductRelation::getProductId).collect(Collectors.toList());
        //根据pId查商品信息
        String json = pmsProductClient.getProductByIds(pIds);
        //解析json
        List<Product> products = JSON.parseArray(json, Product.class);

        //封装为map
        Map<Long,Product> map = new HashMap<>();
        for (Product product : products) {
            map.put(product.getId(),product);
        }

        //封装数据
        for (FlashPromotionProductRelation record : records) {
            Product product = map.get(record.getProductId());
            if (product != null){
                record.setProduct(product);
            }
        }
        return new PageResult(pageNum,pageSize, (int) pageInfo.getTotal(),records);
    }

    /**
     * 修改关联信息
     */
    @Override
    public void updateRelation(Long id, FlashPromotionProductRelation relation) {
        relation.setId(id);
        this.updateById(relation);
    }

    /**
     * 获取管理商品促销信息
     */
    @Override
    public FlashPromotionProductRelation flashProductRelation(Long id) {
        return this.getById(id);
    }


}
